package com.taotao.slink.controller;

import com.taotao.slink.service.ShortLinkService;
import com.taotao.slink.service.VisitStatistics;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author saytime
 * @since 2018-12-06
 */
@Slf4j
@Api(tags = "登录接口")
@Controller
@RequestMapping("/s")
public class JumpController {

    @Autowired
    private ShortLinkService shortLinkService;
    @Autowired
    private VisitStatistics visitStatistics;

    @ApiOperation(value = "模拟短链接跳转", notes = "真正线上应该用openResty来读取redis跳转")
    @ResponseBody
    @GetMapping(value = "/{linkCode}")
    public void redirect(@PathVariable("linkCode") String linkCode, HttpServletResponse response) throws IOException {

        String lk = shortLinkService.getSrcLink(linkCode);
        // 临时重定向
        visitStatistics.incrVisit(LocalDate.now(),linkCode,1);
        response.sendRedirect(lk);
    }


}
